package com.wodsy.phoenix.datadao.shoppingcart.dao.impl;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wodsy.phoenix.commons.utils.MD5Utils;
import com.wodsy.phoenix.datadao.shoppingcart.dao.ShoppingcartRedisDao;
import com.wodsy.phoenix.dataobject.shoppingcart.model.Shoppingcart;


@Component("shoppingcartRedisDao")
public class ShoppingcartRedisDaoImpl  implements ShoppingcartRedisDao {

	@Autowired
    protected RedisTemplate<String, String> redisTemplate;
	
	@Override
	public Shoppingcart getMyShoppingcartById(Long id) {
		return null;
	}

	@Override
	public Shoppingcart getMyShoppingcartByUid(Integer userId) {
		String hashKey = MD5Utils.getMD5String(userId+"");
		String json_re = (String) redisTemplate.opsForValue().get(hashKey);
		JSON json = JSONObject.parseObject(json_re);
		Shoppingcart sc = JSON.toJavaObject(json, Shoppingcart.class);
		return sc;
	}

	@Override
	public Integer addMyShoppingcart(Shoppingcart shoppingcart) {
		String jsonstr = JSONObject.toJSONString(shoppingcart);
		String hashKey = MD5Utils.getMD5String(shoppingcart.getBuyerUserId()+"");
		redisTemplate.opsForValue().set(hashKey, jsonstr);
		return 1;
	}

	@Override
	public void updateShoppingcart(Shoppingcart shoppingcart) {
		String jsonstr = JSONObject.toJSONString(shoppingcart);
		String hashKey = MD5Utils.getMD5String(shoppingcart.getBuyerUserId()+"");
		redisTemplate.opsForValue().set(hashKey, jsonstr);
	}

	@Override
	public void delShoppingcart(Integer userId) {
		String hashKey = MD5Utils.getMD5String(userId+"");
		Set<String> set = redisTemplate.keys(hashKey);
		redisTemplate.delete(set);
	}
    
}
